﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum MoveDir
{
    left,
    right,
    up,
    down,
}
public class Snake : MonoBehaviour {

    //获取蛇头
    public GameObject _head;
    //获取蛇身体集合
    public List<GameObject> _bodyList = new List<GameObject>();
    //获取蛇尾
    public GameObject _tail;

    //获取蛇身体预制物
    public GameObject bodyPrefab;
    //蛇头初始坐标
    private Vector3 _headInitPos = new Vector3(0f, 2f, -0.5f);
    //蛇头的初始旋转
    private Quaternion _headInitRotation = Quaternion.identity;
    //蛇尾初始坐标
    private Vector3 _tailInitPos = new Vector3(0f, 1f, -0.5f);
    //蛇头的旋转
    private Quaternion _headRotation;
    //蛇头的移动方向
    private MoveDir _dir;
    //蛇的状态
    private bool _isDead = false;

    public bool IsDead
    {
        get { return _isDead; }
        set { _isDead = value; }
    }

    void Awake()
    {

    }
    void Start()
    {
        Reset();
    }

    private void Reset()
    {
        _head.transform.position = _headInitPos;
        _headRotation = _headInitRotation;
        _dir = MoveDir.up;
        GetRota();
        _head.transform.rotation = _headRotation;
        _tail.transform.position = _tailInitPos;
        for (int i = 0; i < _bodyList.Count; i++)
        {
            GameObject.Destroy(_bodyList[i]);
        }
        _bodyList.Clear();
        _isDead = false;
    }

    private void Grow()
    {
        GameObject body = GameObject.Instantiate(bodyPrefab);
        body.transform.position = _tail.transform.position;
        body.SetActive(false);
        body.transform.SetParent(this.transform);
        _bodyList.Add(body);
    }

    private void GetRota()
    {
        switch (_dir)
        {
            case MoveDir.left:
                _headRotation = Quaternion.Euler(0, 0, 90);
                break;
            case MoveDir.right:
                _headRotation = Quaternion.Euler(0, 0, -90);
                break;
            case MoveDir.up:
                _headRotation = Quaternion.Euler(0, 0, 0);
                break;
            case MoveDir.down:
                _headRotation = Quaternion.Euler(0, 0, 180);
                break;
            default:
                break;
        }
    }

    private void Move()
    {
        if (_bodyList.Count<=0)
        {
            _tail.transform.position = _head.transform.position;
        }
        else
        {
            _tail.transform.position = _bodyList[_bodyList.Count - 1].transform.position;
            for (int i = _bodyList.Count - 1; i > 0; i--)
            {
                _bodyList[i].transform.position = _bodyList[i - 1].transform.position;
                if (!_bodyList[i].active)
                {
                    _bodyList[i].SetActive(true);
                }
            }
            _bodyList[0].transform.position = _head.transform.position;
            if (!_bodyList[0].active)
            {
                _bodyList[0].SetActive(true);
            }
        }
        GetRota();
        _head.transform.rotation = _headRotation;
        _head.transform.Translate(Vector3.up*1f,Space.Self);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            if (_headRotation == Quaternion.Euler(0, 0, 90) || _headRotation == Quaternion.Euler(0, 0, -90))
            {
                _dir = MoveDir.up;
            }
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            if (_headRotation == Quaternion.Euler(0, 0, 90) || _headRotation == Quaternion.Euler(0, 0, -90))
            {
                _dir = MoveDir.down;
            }
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (_headRotation == Quaternion.Euler(0, 0, 0) || _headRotation == Quaternion.Euler(0, 0, 180))
            {
                _dir = MoveDir.left;
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            if (_headRotation == Quaternion.Euler(0, 0, 0) || _headRotation == Quaternion.Euler(0, 0, 180))
            {
                _dir = MoveDir.right;
            }
        }
        if (Input.GetKey(KeyCode.RightControl))
        {
            Time.timeScale = RealManager._instance._level*2;
        }
        if (Input.GetKeyUp(KeyCode.RightControl))
        {
            Time.timeScale = RealManager._instance._level;
        }
    }
    void FixedUpdate()
    {
        if (!_isDead)
        {
            Move();
        }
    }

    void ChangeState()
    {
        _isDead = _isDead ? false : true;
    }

}
